<template>
  <div class="td-doctor-me-appointment-time-edit">
    <mt-header fixed title="预约时间设置">
      <router-link to="tdDoctorMeAppointmentTime" slot="left">
        <mt-button icon="back"></mt-button>
      </router-link>
    </mt-header>

    <mt-cell title="挂职医院" is-link @click.native="showHospital=true">
      <span v-if="!HospitalID">请选择</span>
      <span v-if="HospitalID">{{hospitalList.find(i=>i.value===HospitalID).label}}</span>
    </mt-cell>
    <mt-cell title="预约时间">
      <el-date-picker
        v-model="params.AppointmentDate"
        style="width:130px;border:none !important;"
        :editable="false"
        size="mini"
        type="date"
        placeholder="选择日期"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        :picker-options="pickerOptions1"
      ></el-date-picker>
    </mt-cell>

    <mt-field label="上午预约人数" placeholder="请输入人数" v-model="params.AMNumber">&nbsp;人</mt-field>
    <mt-field label="下午预约人数" placeholder="请输入人数" v-model="params.PMNumber">&nbsp;人</mt-field>
    <mt-field label="晚上预约人数" placeholder="请输入人数" v-model="params.NMNumber">&nbsp;人</mt-field>

    <div class="btnSubmit">
      <el-button
        type="primary"
        class="td-btn-doctor-primary"
        style="width:80%;"
        round
        @click.native="btnSave();"
      >保存</el-button>
    </div>

    <!--挂职医院-->
    <mt-popup v-model="showHospital" position="bottom" class="mint-popup-box">
      <mt-header title="挂职医院">
        <mt-button slot="right" @click.native="showHospital=false">关闭</mt-button>
        <mt-button slot="left" @click.native="showHospital=false">确定</mt-button>
      </mt-header>
      <div class="list-box">
        <mt-radio :options="hospitalList" v-model="HospitalID" @change="getHospitalID"></mt-radio>
      </div>
    </mt-popup>
  </div>
</template>

<script>
import DoctorAppointmentService from "../../../../_services/doctorAppointment.service";
export default {
  name: "TianDingDoctorMeAppointmentTimeEditComponent",
  data() {
    return {
      user: {},
      showHospital: false,
      HospitalID: "",
      hospitalList: [],
      pickerOptions1: {
        disabledDate: time => {
          return this.dealDisabledDate(time);
        }
      },
      params: {
        AppointmentID: 0,
        DoctorID: 0,
        HospitalID: 0,
        AppointmentDate: "",
        AMNumber: 0,
        PMNumber: 0,
        NMNumber: 0
      }
    };
  },
  mounted() {
    this.user = this.utils$.dataFormat(
      JSON.parse(this.local$.getItem("tdUser"))
    );
    if (this.utils$.isEmpty(this.local$.getItem("tdAppointment"))) {
      this.params = JSON.parse(this.local$.getItem("tdAppointment"));
    }
    if (this.user.hospitalList.length > 0) {
      this.user.hospitalList.forEach(element => {
        let item = {
          value: element.HospitalID.toString(),
          label: element.HospitalName
        };
        this.HospitalID = item.value;
        this.hospitalList.push(item);
      });
    }
  },
  methods: {
    dealDisabledDate(time) {
      // time.getTime是把选中的时间转化成自1970年1月1日 00:00:00 UTC到当前时间的毫秒数
      // Date.now()是把今天的时间转化成自1970年1月1日 00:00:00 UTC到当前时间的毫秒数,这样比较好比较
      // return的值,true是不可以操作选择,false可以操作选择,比如下面这个判断就只能选择今天之后的时间
      return time.getTime() < Date.now();

      // 这里减8.64e7的作用是,让今天的日期可以选择,如果不减的话,今天的日期就不可以选择,判断中写<= 也是没用的,一天的毫秒数就是8.64e7
      //return time.getTime() < Date.now()
      // return time.getTime() < Date.now() - 8.64e7
    },
    btnSave() {
      if (!this.utils$.isEmpty(this.params.AppointmentDate)) {
        this.toast$("请选择预约时间");
        return;
      }

      if (!this.utils$.isEmpty(this.params.AMNumber)) {
        this.toast$("请输入上午可预约人数");
        return;
      }

      if (!this.utils$.isEmpty(this.params.PMNumber)) {
        this.toast$("请输入下午可预约人数");
        return;
      }

      if (!this.utils$.isEmpty(this.params.NMNumber)) {
        this.toast$("请输入晚上可预约人数");
        return;
      }

      this.indicator$.open("请稍后...");

      let params = {
        AppointmentID: this.params.AppointmentID,
        DoctorID: this.user.DoctorID,
        HospitalID: parseInt(this.HospitalID),
        AppointmentDate: this.params.AppointmentDate,
        AMNumber: parseInt(this.params.AMNumber),
        PMNumber: parseInt(this.params.PMNumber),
        NMNumber: parseInt(this.params.NMNumber)
      };

      if (params.AppointmentID > 0) {
        DoctorAppointmentService.updateAppointment(params).then(response => {
          if (response !== null && response.Data > 0) {
            this.toast$("保存成功");
          } else {
            this.toast$("保存失败");
          }
          this.indicator$.close();
          this.$router.push({ name: "tdDoctorMeAppointmentTime" });
        });
      } else {
        DoctorAppointmentService.addAppointment(params).then(response => {
          if (response !== null && response.Data > 0) {
            this.toast$("保存成功");
          } else {
            this.toast$("保存失败");
          }
          this.indicator$.close();
          this.$router.push({ name: "tdDoctorMeAppointmentTime" });
        });
      }
    },
    getHospitalID(event) {
      this.showHospital = false;
    }
  }
};
</script>

